package com.wn.service.impl;

import com.wn.bean.Building;
import com.wn.bean.PageVo;
import com.wn.exception.BuildDeleteException;
import com.wn.mapper.BuildingMapper;
import com.wn.mapper.HouseMapper;
import com.wn.service.IBuildingService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Collections;
import java.util.List;

/**
 * @author yellow Docter
 * @date 2024-06-27
 * @desc 蜗牛创想：www.woniuxy.com
 */
@Service
public class BuildServiceImpl implements IBuildingService {

    @Autowired
    private BuildingMapper buildingMapper;

    @Autowired
    private HouseMapper houseMapper;

    @Override
    public boolean removeBuild(int id) throws BuildDeleteException {

        //在删除前面进行判断  需要检查 house中是否有对应的引用
        int count = houseMapper.countByBuild(id);

        if (count > 0) {
                throw new BuildDeleteException("当前的数据被引用、不能删除");
        }

        int row = buildingMapper.deleteBuildById(id);

        return row > 0 ;
    }

    @Override
    public boolean updateBuildingById(Building building) {
        int row = buildingMapper.updateBuildById(building);
        return row > 0 ;
    }

    @Override
    public boolean addBuilding(Building building) {
        int row = buildingMapper.addBuild(building);
        return row > 0 ;
    }

    @Override
    public PageVo pageBuild(int currentPage, int pageSize) {

        //01 调用mapper 查询 总数
        int total = buildingMapper.count();

        //02 调用mapper 查询 分页的数据
        //select * from build limit start,size
        int start = (currentPage -1) * pageSize;
        List<Building> list = buildingMapper.pageBuilds(start,pageSize);

        //03 创建PageVo 组装vo对象
        PageVo pageVo = new PageVo();
        pageVo.setData(list);
        //把 int 转成Long类型
        pageVo.setTotal(Long.valueOf(String.valueOf(total)));

        return pageVo;
    }

    @Override
    public List<Building> listAll() {
        return buildingMapper.selectAll();
    }
}
